package com.ms.seguridad.bean;


import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ModuleBean implements Serializable {
    transient Logger log = Logger.getLogger(getClass());
    private static final long serialVersionUID = -7157751028665424737L;
    private BigDecimal moduleCode;
    private ApplicationBean applicationBean;
    private ModuleBean moduleBean;
    private String moduleName;
    private String moduleLevel;
    private String moduleUrl;
    private String moduleActiveFlg;
    private boolean enabled = true;
    private BigDecimal moduleOrder;
    private List<String> parameterList = new ArrayList<String>();
    private String creativeUser;
    private Timestamp creationDate;
    private String modifierUser;
    private Timestamp modificationDate;
    private List<ModuleBean> moduleList = new ArrayList<ModuleBean>();

    public ModuleBean() {
    }

    public ModuleBean(String moduleName, String moduleUrl) {
        this.moduleName = moduleName;
        this.moduleUrl = moduleUrl;
    }

    public ModuleBean addSubModule(ModuleBean subModuleBean) {
        if (!moduleAlreadyExist(subModuleBean)) {
            moduleList.add(subModuleBean);
        }
        return this;
    }

    private boolean moduleAlreadyExist(ModuleBean newModule) {
        for (ModuleBean moduleBean : moduleList) {
            String newModuleUrl = newModule.getModuleUrl();
            if (StringUtils.hasText(newModuleUrl) && newModule.getModuleUrl().equals(moduleBean.getModuleUrl())) {
                return true;
            }
        }
        return false;
    }

    public ModuleBean setParamList(String... paramList) {
        this.parameterList = Arrays.asList(paramList);
        return this;
    }

    public BigDecimal getModuleCode() {
        return this.moduleCode;
    }

    public void setModuleCode(BigDecimal moduleCode) {
        this.moduleCode = moduleCode;
    }

    public ApplicationBean getApplicationBean() {
        return this.applicationBean;
    }

    public void setApplicationBean(ApplicationBean applicationBean) {
        this.applicationBean = applicationBean;
    }

    public ModuleBean getModuleBean() {
        return this.moduleBean;
    }

    public void setModuleBean(ModuleBean moduleBean) {
        this.moduleBean = moduleBean;
    }

    public String getModuleName() {
        return this.moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getModuleLevel() {
        return this.moduleLevel;
    }

    public void setModuleLevel(String moduleLevel) {
        this.moduleLevel = moduleLevel;
    }

    public String getModuleUrl() {
        return this.moduleUrl;
    }

    public void setModuleUrl(String moduleUrl) {
        this.moduleUrl = moduleUrl;
    }

    public String getModuleActiveFlg() {
        return moduleActiveFlg;
    }

    public void setModuleActiveFlg(String moduleActiveFlg) {
        this.moduleActiveFlg = moduleActiveFlg;
    }

    public BigDecimal getModuleOrder() {
        return this.moduleOrder;
    }

    public void setModuleOrder(BigDecimal moduleOrder) {
        this.moduleOrder = moduleOrder;
    }

    public List<String> getParameterList() {
        return parameterList;
    }

    public void setParameterList(List<String> parameterList) {
        this.parameterList = parameterList;
    }

    public String getCreativeUser() {
        return this.creativeUser;
    }

    public void setCreativeUser(String creativeUser) {
        this.creativeUser = creativeUser;
    }

    public Timestamp getCreationDate() {
        return this.creationDate;
    }

    public void setCreationDate(Timestamp creationDate) {
        this.creationDate = creationDate;
    }

    public String getModifierUser() {
        return this.modifierUser;
    }

    public void setModifierUser(String modifierUser) {
        this.modifierUser = modifierUser;
    }

    public Timestamp getModificationDate() {
        return this.modificationDate;
    }

    public void setModificationDate(Timestamp modificationDate) {
        this.modificationDate = modificationDate;
    }

    public List<ModuleBean> getModuleList() {
        return this.moduleList;
    }

    public void setModuleList(List<ModuleBean> moduleList) {
        this.moduleList = moduleList;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ModuleBean that = (ModuleBean) o;

        if (!moduleName.equals(that.moduleName)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return moduleName.hashCode();
    }

    public static ModuleBean getInstanceForSearch(String moduleName) {
        ModuleBean moduleBean = new ModuleBean();
        moduleBean.setModuleName(moduleName);
        return moduleBean;
    }

/*
    public void addSubModules(ModuleType... modulesType) {
        for (ModuleType moduleType : modulesType) {
            if (moduleType.isEnabled()) {
                ModuleBean moduleBean = buildFrom(moduleType);
                addSubModule(moduleBean);
            }
        }
    }

    public static ModuleBean buildFrom(ModuleType moduleType) {
        return new ModuleBean(moduleType.getModuleName(), moduleType.getModuleUrl());
    }
*/

    public ModuleBean getSubmoduleFor(String moduleName) {
        StringBuffer buf = new StringBuffer();
        for (ModuleBean subModule : moduleList) {
            buf.append(subModule.getModuleName()).append(",");
            if (moduleName.equals(subModule.getModuleName())) {
                return subModule;
            }
        }
        log.error("No existe submodulo:'"+moduleName+"'  existentes:"+buf.toString());
        return null;
    }

    public String toString() {
        return moduleName + "-->" + moduleUrl;
    }
}